/**
 * 
 */

package com.kugou.game.sdk.ui.activity;

import com.kugou.framework.v4.Fragment;
import com.kugou.game.sdk.base.BaseCommonTitleFragmentActivity;
import com.kugou.game.sdk.utils.Rx;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;

/**
 * 描述:通用界面
 *
 * @author jerryliu
 * @since 2015年8月14日 上午10:21:53
 */
public class CommonSdkActivity extends BaseCommonTitleFragmentActivity {

    public static final String INTENT_FRAGMENT_NAME = "fragmentName";

    public static final String INTENT_PAGE_TITLE = "pageTitle";

    private OnBackPressedListener mBackPressedListener;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        int layoutId = Rx.layout.kg_activity_sdk_common;
        setContentView(layoutId);
        handleIntent();
    }

    public void setBackPressedListener(OnBackPressedListener mBackPressedListener) {
        this.mBackPressedListener = mBackPressedListener;
    }

    private void handleIntent() {
        Intent intent = getIntent();
        int titleId = intent.getIntExtra(INTENT_PAGE_TITLE, 0);
        if (titleId != 0) {
            setCommonTitle(titleId);
        }

        try {
            String fragmentName = intent.getStringExtra(INTENT_FRAGMENT_NAME);
            if (!TextUtils.isEmpty(fragmentName)) {
                Fragment content = (Fragment) Class.forName(fragmentName).newInstance();
                Bundle args = getArgumentsFromIntent();
                content.setArguments(args);
                if (content != null) {
                    getSupportFragmentManager().beginTransaction().add(Rx.id.kg_container, content)
                            .commitAllowingStateLoss();
                }
            } else {
                throw new RuntimeException("please input correct target fragment name!!");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private Bundle getArgumentsFromIntent() {
        return null;
    }

    /**
     * 创建跳转Intent
     * 
     * @param context
     * @param targetFragmentName
     * @param targetPageTitleResId
     * @return
     */
    public static Intent creatIntent(Context context, String targetFragmentName,
            int targetPageTitleResId) {
        Intent intent = new Intent(context, CommonSdkActivity.class);
        intent.putExtra(INTENT_FRAGMENT_NAME, targetFragmentName);
        intent.putExtra(INTENT_PAGE_TITLE, targetPageTitleResId);
        return intent;
    }

    @Override
    protected void onBackBtnClick() {
        if (mBackPressedListener != null) {
            mBackPressedListener.onActivityBack();
        } else {
            super.onBackBtnClick();
        }
    }

    @Override
    public void onBackPressed() {
        if (mBackPressedListener != null) {
            mBackPressedListener.onActivityBack();
        } else {
            super.onBackPressed();
        }
    }

    /** 页面回退操作监听器 */
    public interface OnBackPressedListener {
        public void onActivityBack();
    }
}
